/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MWmi_Win32_DiskPartition.h"

//---------------------------------------------------------------------------

MWmi_Win32_DiskPartition::MWmi_Win32_DiskPartition(void)
{
	propertyCount = 11;
	Initialize();
}

MWmi_Win32_DiskPartition::~MWmi_Win32_DiskPartition(void)
{
	DeleteChild();
}

//---------------------------------------------------------------------------

MWmiInfo* MWmi_Win32_DiskPartition::CreateChild(void)
{
	MWmi_Win32_DiskPartition *elem;

	try{
		elem = new MWmi_Win32_DiskPartition;
	}
	catch(...){
		elem = NULL;
	}
	return elem;
}

void MWmi_Win32_DiskPartition::DeleteChild(void)
{
	MWmi_Win32_DiskPartition *elem;
	
	if(next!=NULL){
		elem = (MWmi_Win32_DiskPartition*)next;
		next = NULL;
		delete elem;
	}
}

//---------------------------------------------------------------------------

void MWmi_Win32_DiskPartition::Initialize(void)
{
	xBootable         = false;
	xBootPartition    = false;
	xDescription      = _T("");
	xDiskIndex        = 0;
	xIndex            = 0;
	xName             = _T("");
	xNumberOfBlocks   = _T("");
	xPrimaryPartition = false;
	xSize             = _T("");
	xStartingOffset   = _T("");
	xType             = _T("");

}

//---------------------------------------------------------------------------

CString MWmi_Win32_DiskPartition::GetQueryString(void)
{
	CString str;

	str = _T("select * from Win32_DiskPartition");
	return str;
}

//---------------------------------------------------------------------------

CString MWmi_Win32_DiskPartition::GetPropertyName(int idxIn)
{
	CString str;

	switch(idxIn){
		case 0 :
			str = _T("Bootable");
			break;
		case 1 :
			str = _T("BootPartition");
			break;
		case 2 :
			str = _T("Description");
			break;
		case 3 :
			str = _T("DiskIndex");
			break;
		case 4 :
			str = _T("Index");
			break;
		case 5 :
			str = _T("Name");
			break;
		case 6 :
			str = _T("NumberOfBlocks");
			break;
		case 7 :
			str = _T("PrimaryPartition");
			break;
		case 8 :
			str = _T("Size");
			break;
		case 9 :
			str = _T("StartingOffset");
			break;
		case 10 :
			str = _T("Type");
			break;

		default :
			str = _T("");
			break;
	}

	return str;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_DiskPartition::SetPropertyValue(int idxIn, VARIANT *vtIn)
{
	if(vtIn==NULL) return true;
	if(vtIn->vt==VT_NULL) return true;

	switch(idxIn){
		case 0 :
			if(vtIn->vt!=VT_BOOL) return false;
			xBootable = (vtIn->boolVal==VARIANT_TRUE ? true : false);
			break;
		case 1 :
			if(vtIn->vt!=VT_BOOL) return false;
			xBootPartition = (vtIn->boolVal==VARIANT_TRUE ? true : false);
			break;
		case 2 :
			if(vtIn->vt!=VT_BSTR) return false;
			xDescription = vtIn->bstrVal;
			break;
		case 3 :
			if(vtIn->vt!=VT_I4) return false;
			xDiskIndex = vtIn->lVal;
			break;
		case 4 :
			if(vtIn->vt!=VT_I4) return false;
			xIndex = vtIn->lVal;
			break;
		case 5 :
			if(vtIn->vt!=VT_BSTR) return false;
			xName = vtIn->bstrVal;
			break;
		case 6 :
			if(vtIn->vt!=VT_BSTR) return false;
			xNumberOfBlocks = vtIn->bstrVal;
			break;
		case 7 :
			if(vtIn->vt!=VT_BOOL) return false;
			xPrimaryPartition = (vtIn->boolVal==VARIANT_TRUE ? true : false);
			break;
		case 8 :
			if(vtIn->vt!=VT_BSTR) return false;
			xSize = vtIn->bstrVal;
			break;
		case 9 :
			if(vtIn->vt!=VT_BSTR) return false;
			xStartingOffset = vtIn->bstrVal;
			break;
		case 10 :
			if(vtIn->vt!=VT_BSTR) return false;
			xType = vtIn->bstrVal;
			break;

		default :
			return false;
	}

	return true;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_DiskPartition::SetFromPointer(MWmiInfo* wmiInfoIn)
{
	MWmi_Win32_DiskPartition *elem;
	MWmi_Win32_DiskPartition *elemNew;

	DeleteChild();
	Initialize();
	if(wmiInfoIn==NULL) return true;

	elem = (MWmi_Win32_DiskPartition*)wmiInfoIn;

	xBootable         = elem->xBootable;
	xBootPartition    = elem->xBootPartition;
	xDescription      = elem->xDescription;
	xDiskIndex        = elem->xDiskIndex;
	xIndex            = elem->xIndex;
	xName             = elem->xName;
	xNumberOfBlocks   = elem->xNumberOfBlocks;
	xPrimaryPartition = elem->xPrimaryPartition;
	xSize             = elem->xSize;
	xStartingOffset   = elem->xStartingOffset;
	xType             = elem->xType;

	if(elem->next==NULL) return true;

	try{
		elemNew = new MWmi_Win32_DiskPartition;
	}
	catch(...){
		elemNew = NULL;
	}
	if(elemNew==NULL) return false;

	next = elemNew;

	return elemNew->SetFromPointer(elem->next);
}

//---------------------------------------------------------------------------

bool MWmi_Win32_DiskPartition::IsEmpty(void)
{
	return xName.IsEmpty() ? true : false;
}

//---------------------------------------------------------------------------
